﻿Namespace Catalog
    Public Class Image
        Public Property Url As Uri
        Public Property Width As Integer
        Public Property Height As Integer
        Public Property Type As ImageType
        Public Property IsDynamic As Boolean

        Public ReadOnly Property DynamicUrl(width As Integer, height As Integer) As Uri
            Get
                If Not Me.IsDynamic Then
                    Return Nothing
                End If

                Return New Uri(String.Format(Me.Url.ToString.Replace("{w}", "{0}").Replace("{h}", "{1}").Replace("{hq}", ""), width, height))
            End Get
        End Property

        Public Shared Function FromBPObject(img As BPOImage) As Image
            Dim newImage As New Image
            newImage.Url = New Uri(img.url)
            newImage.Width = img.width
            newImage.Height = img.height
            Return newImage
        End Function

        Public Shared Function FromBPObject(img As KeyValuePair(Of String, BPOImage)) As Image
            Dim newImage As Image = Image.FromBPObject(img.Value)

            Select Case img.Key
                Case "small"
                    newImage.Type = ImageType.Small
                Case "medium"
                    newImage.Type = ImageType.Medium
                Case "large"
                    newImage.Type = ImageType.Large
                Case "xlarge"
                    newImage.Type = ImageType.ExtraLarge
                Case "waveform"
                    newImage.Type = ImageType.Waveform
                Case "main"
                    newImage.Type = ImageType.Main
                Case Else
                    newImage.Type = ImageType.Undefined
            End Select

            Return newImage
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("{0} ({1}x{2})", Me.Type, Me.Width, Me.Height)
        End Function
    End Class

    Public Enum ImageType
        Undefined
        Small
        Medium
        Large
        ExtraLarge
        Waveform
        Main
    End Enum
End Namespace